#include "widget.h"
#include "ui_widget.h"
#include "caculator.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void getButtonText(QPushButton* button,char * formula){
    char *str = (button->text()).toLatin1().data();
    if(strcmp(formula,"InPut Formula")==0){
        strcpy(formula, str);

    }else{
        strcat(formula, str);

    }
    return ;

}

void Widget::on_buttonDel_clicked()
{
    if(strcmp(formula, "InPut Formula")==0)
    {
        return ;
    }
    result = 0;
    for(int i = 0;i<500;i++)
    {
        if(formula[i]=='\0')//指向了字符串的最后一个非空字符。
        {
            formula[i-1]='\0';
            break;
        }

    }
    ui->strLabel->setText(formula);
}
void Widget::on_buttonClr_clicked()//清除
{
    QString str ="清楚数据";
    qDebug()<< str;
    result = 0;
    strcpy(formula, "InPut Formula");
    ui->strLabel->setText(formula);
}

void Widget::on_buttonCal_clicked()//计算结果
{
    if(strcmp(formula,"InPut Formula")==0){
        return;
    }else{
        QString str ="根据算式得到结果";
//        strcpy(formula,"1+2*3+(4*5+6)*7");
        qDebug()<< str << formula;
        double result = getResultByString(formula);
        ui->resLabel->setText(QString::number(result));
        strcpy(formula, "InPut Formula");
    }
}

//按钮1-0
void Widget::on_button0_clicked()
{
    getButtonText(ui->button0,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_button1_clicked()
{
    getButtonText(ui->button1,formula);
    ui->strLabel->setText(formula);
}

void Widget::on_button2_clicked()
{
    getButtonText(ui->button2,formula);
    ui->strLabel->setText(formula);
}

void Widget::on_button3_clicked()
{
    getButtonText(ui->button3,formula);
    ui->strLabel->setText(formula);
}

void Widget::on_button4_clicked()
{
    getButtonText(ui->button4,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_button5_clicked()
{
    getButtonText(ui->button5,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_button6_clicked()
{
    getButtonText(ui->button6,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_button7_clicked()
{
    getButtonText(ui->button7,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_button8_clicked()
{
    getButtonText(ui->button8,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_button9_clicked()
{
    getButtonText(ui->button9,formula);
    ui->strLabel->setText(formula);
}

void Widget::on_buttonPot_clicked()//小数点
{
    getButtonText(ui->buttonPot,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_buttonAdd_clicked()//加法
{
    getButtonText(ui->buttonAdd,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_buttonSub_clicked()//减法
{
    getButtonText(ui->buttonSub,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_buttonMul_clicked()//乘法
{
    getButtonText(ui->buttonMul,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_buttonDiv_clicked()//除法
{
    getButtonText(ui->buttonDiv,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_buttonRem_clicked()//取余数
{
    getButtonText(ui->buttonRem,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_buttonLft_clicked()//左括号
{
    getButtonText(ui->buttonLft,formula);
    ui->strLabel->setText(formula);
}
void Widget::on_buttonRig_clicked() //右括号
{
    getButtonText(ui->buttonRig,formula);
    ui->strLabel->setText(formula);
}

